
#include"raycaster.h"
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include"algfloat.h"
#include"ray.h"

void RayCaster::Cast (Ray * ray, int x, int y, bool aa) const
{
  float xl = (2*(x+0.5)/(float)nx - 1)* ratio;
  float yl =  2*(y+0.5)/(float)ny - 1;
  if(aa){
    float pxsize=1.0/nx;
    float pysize=1.0/ny;
    float divxsize = (pxsize/pdiv);
    float divysize = (pysize/pdiv);
    xl+=-pxsize/2.0 + divxsize * ray->pposx + divxsize * (float)rand()/RAND_MAX ;
    yl+=-pysize/2.0 + divysize * ray->pposy + divysize * (float)rand()/RAND_MAX ;
    //xl+=-pxsize/2.0 + divxsize * ray->pposx + divxsize * 0.5 ;
    //yl+=-pysize/2.0 + divysize * ray->pposy + divysize * 0.5 ;
    ray->pposx = (ray->pposx+1)%pdiv;
    ray->pposy = ray->pposx==0?(ray->pposy+1)%pdiv:ray->pposy;
  }
  //ponto inicial na origem
  float * p = ray->GetP(VIEW_RAY);
  p[0]=0.0;
  p[1]=0.0;
  p[2]=0.0;
  p[3]=1.0;
  //direcao
  float * d = ray->GetD(VIEW_RAY);
  d[0]=xl;
  d[1]=yl;
  d[2]=-s;
  d[3]=0.0;
  AFUnit(d);
  ray->tmax = -1.0;
  ray->tmin = -1.0;
  ray->shadow_obj = -1.0;
  ray->shadow_mode = false;
  float * color = ray->color;
  color[0]=0.0;
  color[1]=0.0;
  color[2]=0.0;
  ray->shade_complete=0.0;
}

